﻿using LoveNote.Application.Picture.ViewModels;
using MahApps.Metro.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using TookKit.Mvvm.Attributes;
using TookKit.Mvvm.Enums;

namespace LoveNote.Application.Picture.Views
{
    [DependencyInjection(Lifetime.Singleton)]
    [View]
    public partial class PreviewView : MetroWindow
    {
        private bool isMouseDown = false;
        private Point mousePosition = new Point();
        private TranslateTransform translateTransform = new TranslateTransform();
        private ScaleTransform scaleTransform = new ScaleTransform();
        private TransformGroup transformGroup = new TransformGroup();
        public PreviewView()
        {
            InitializeComponent();
            transformGroup.Children.Add(scaleTransform);//定义缩放
            transformGroup.Children.Add(translateTransform);//定义移动
            image.RenderTransform = transformGroup;//定义变换组
            canvas.Loaded += (s, e) =>
            { 
                //初始化border控件大小适配canvas父容器
                if (image.Width > canvas.ActualWidth || image.Height > canvas.ActualHeight)
                {
                    var scale = Math.Min(canvas.ActualWidth / image.Width, canvas.ActualHeight / image.Height);
                    scaleTransform.ScaleX = scale;
                    scaleTransform.ScaleY = scale;
                }

                //将图片显示到正中央
                var left = (canvas.ActualWidth - image.ActualWidth * scaleTransform.ScaleX) / 2;
                var top = (canvas.ActualHeight - image.ActualHeight * scaleTransform.ScaleY) / 2;
                translateTransform.X = left;
                translateTransform.Y = top;

            };
        }

        private void Canvas_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            isMouseDown = true;
            mousePosition = e.GetPosition(canvas);
        }
        private void Canvas_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
        {
            Close();
            isMouseDown = false;
        }
        private void Canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            isMouseDown = true;
            mousePosition = e.GetPosition(canvas);
        }
        private void Canvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            isMouseDown = false;
        }
        private void Canvas_MouseMove(object sender, MouseEventArgs e)
        {
            var position = e.GetPosition(canvas);
            if (isMouseDown)
            {
                TranslateTransform translateTransform = transformGroup.Children[1] as TranslateTransform;
                translateTransform.X += position.X - mousePosition.X;
                translateTransform.Y += position.Y - mousePosition.Y;
                mousePosition = position;
            }
        }
        private void Canvas_MouseWheel(object sender, MouseWheelEventArgs e)
        {            
            double scale = e.Delta * 0.001;
            var position = e.GetPosition(canvas);
            Point inversePoint = transformGroup.Inverse.Transform(position);
            ScaleTransform scaleTransform = transformGroup.Children[0] as ScaleTransform;
            TranslateTransform translateTransform = transformGroup.Children[1] as TranslateTransform;
            if (scaleTransform.ScaleX + scale < 0.1) return;
            scaleTransform.ScaleX += scale;
            scaleTransform.ScaleY += scale;
            translateTransform.X = -1 * ((inversePoint.X * scaleTransform.ScaleX) - position.X);
            translateTransform.Y = -1 * ((inversePoint.Y * scaleTransform.ScaleY) - position.Y);
            var vm = DataContext as PreviewViewModel;
            vm.Zoom = Math.Round(scaleTransform.ScaleX, 2);
        }
    }
}
